<template>
	<view style="padding: 20rpx;">
		
		<view class="mainView" style="display: flex;justify-content: space-between; align-items: center;  height: 10vh;padding:20rpx;">
			<view style="width: 400rpx;font-size: 40rpx;font-weight: bold;color: #5597FB;">{{deptData.deptName }}排班计划</view>
			<view style="width: 150rpx;">
				<u-button  type="primary" size="medium" shape="circle" @click="showAdd=true;">新增排班</u-button>
			</view>
			
		</view>
		
		<view class="mainView" style="padding: 20rpx;margin-top: 50rpx;">
			<view v-for="(item,index) in planList" :key="index" style="padding: 20rpx;border-bottom: 1px solid gray;border-top: 1px solid gray;">
				<view style="display: flex;justify-content: space-between;padding: 20rpx;">
					<view style="display: flex;justify-content: start;"><u-icon name="grid-fill" color="#5597FB" style="margin-right: 20rpx;"></u-icon>{{item.drName}}</view>
					
					<view style="color: red;" @click="delPlan(item)">X</view>
				</view>
				<view style="padding: 20rpx;">
					<view style="color: gray;">坐诊开始：{{ item.workTimeBegin }}</view>
					<view style="color: gray;">坐诊结束：{{ item.workTimeEnd }}</view>
				</view>
			</view>
		</view>
		
		<u-modal :show="showAdd"  title="新增排班" showCancelButton="true" @cancel="showAdd=false" @confirm="subPlan">
			<view class="slot-content">
				<view style="width: 100%;min-width: 600rpx;">
					<u-cell-group>
						<u-cell title="坐诊医生" @click="showUserSel=true;" :value="addPlan.drName||'请选择'" :isLink="true"></u-cell>
						<u-cell title="坐诊开始" @click="setTimeSel('workTimeBegin');" :value="addPlan.workTimeBegin||'请选择'" :isLink="true"></u-cell>
						<u-cell title="坐诊结束" @click="setTimeSel('workTimeEnd');" :value="addPlan.workTimeEnd||'请选择'" :isLink="true"></u-cell>
					</u-cell-group>
				</view>
				
			</view>
		</u-modal>
		
		<u-datetime-picker :minDate="min" @confirm="confirm"
		                :show="showTime"
		                mode="datetime" :value="time"
		        ></u-datetime-picker>
				
		<u-picker :show="show" :columns="columns"></u-picker>
		<u-action-sheet :actions="userList" @select="selectClick" title="选择医生" :show="showUserSel"></u-action-sheet>
	</view>
</template>

<script>
	import appRequest from "@/common/appRequestUrl.js";
	export default {
		data() {
			return {
				min:new Date().getTime(),
				showUserSel:false,
				showAdd:false,
				showTime:false,
				deptData:{
					id:0,
					deptName:""
				},
				time: Number(new Date()),
				addPlan:{
					drFk:0,
					drName:"",
					deptFk:0,
					deptName:"",
					workTimeBegin:"",
					workTimeEnd:"",
					jobTitle:""
				},
				planList:[],
				userList:[],
				selTimeName:""
			};
		},onLoad:function(options){
			this.user = appRequest.getUserInfo();
			if(!this.user){
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			if(options.id){
				this.deptData.id = options.id;
				this.deptData.deptName = options.deptName;
				this.addPlan.deptFk = options.id;
				this.addPlan.deptName = options.deptName;
				this.getData();
				this.getUserData();
			}
		},
		methods: {
			delPlan(item){
				let _this = this;
				uni.showModal({
					title:"信息",
					content:"确认删除?",
					success:function(res){
						if(res.confirm){
							item.validFlag = 0;
							appRequest.request({
								method: "POST",
								data: item,
								url: appRequest.urlMap.editNmHosiptalScheduling,
								success: function(res) {
									if(res.data.code == 200){
										_this.$api.msg("删除成功");
										_this.getData();
									}else{
										_this.$api.msg(res.data.msg);
									}
								},
								fail: function(res) {
									_this.$api.msg("请求异常");
								}
							})
						}
					}
				})
			},
			subPlan(){
				let _this = this;
				appRequest.request({
					method: "POST",
					data: this.addPlan,
					url: appRequest.urlMap.addNmHosiptalScheduling,
					success: function(res) {
						if(res.data.code == 200){
							_this.$api.msg("添加成功");
							_this.getData();
							_this.showAdd = false;
							_this.addPlan ={
								drFk:0,
								drName:"",
								deptFk:0,
								deptName:"",
								workTimeBegin:"",
								workTimeEnd:"",
								jobTitle:""
							}
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
			},
			selectClick(res){
				this.addPlan.drFk=res.uid;
				this.addPlan.drName=res.realName;
				this.addPlan.jobTitle=res.jobTitle;
				this.showUserSel = false;
			},
			setTimeSel(name){
				this.showTime= true;
				this.selTimeName = name;
			},
			confirm(res){
				this.addPlan[this.selTimeName] = this.formatTimestamp(res.value);
				this.showTime = false;
			},
			formatTimestamp(timestamp) {
			  const date = new Date(timestamp);
			  
			  // 获取年、月、日、时、分
			  const year = date.getFullYear();
			  const month = String(date.getMonth() + 1).padStart(2, '0');
			  const day = String(date.getDate()).padStart(2, '0');
			  const hours = String(date.getHours()).padStart(2, '0');
			  const minutes = String(date.getMinutes()).padStart(2, '0');
			  
			  // 组合成需要的格式
			  return `${year}-${month}-${day} ${hours}:${minutes}`;
			},
			radioChange(evt){
				this.current = evt.detail.value;
				this.form.sex = evt.detail.value;
			},
			getData(){
				let _this = this;
				appRequest.request({
					method: "POST",
					data: {
						validFlag:1,
						deptFk:this.deptData.id
					},
					url: appRequest.urlMap.findNmHosiptalSchedulingList,
					success: function(res) {
						if(res.data.code == 200){
							_this.planList = res.data.data;
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
				
			},getUserData(){
				let _this = this;
				appRequest.request({
					method: "POST",
					data: {
						validFLag:1,
						type:3
					},
					url: appRequest.urlMap.findSysUserList,
					success: function(res) {
						if(res.data.code == 200){
							_this.userList = res.data.data;
							_this.userList.map(function(item,index){
								item['name'] = item.realName;
								item['subname'] = item.jobTitle;
							})
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.mainView {
		border-radius: 8px;
		box-shadow: 2px 2px 5px 1px lightgray;
	}
</style>
